<template>
    <el-select v-model="form.id" clearable :loading="loading" @visible-change="onGetData" @change="onResourceSelect">
        <el-option label="请选择" :value="0" />
        <el-option v-for="item in data" :key="item.id" :label="item.title" :value="item.id" />
    </el-select>
</template>

<script lang="ts" setup>
import { ref,onMounted, watch } from 'vue'
import http from '../util/request';
interface Project {
    id: number
    title: string
}
const data = ref<Project[]>([])
const loading = ref<boolean>(false)
const form = ref<any>({id:0,type:''})
const props = defineProps<{
    data:any
    type:string
}>()

watch(props,()=>{
    form.value.id = props.data
    form.value.type = props.type
},{ immediate: true })

const onGetData = () => {
    if(data.value.length > 0){
        return
    }
    
    loading.value = true
    http.post('/resource',form.value).then((response:any)=>{
        loading.value = false
        data.value = response.data.data
    })
}

const onResourceSelect = () => {
    emit('onResourceSelect',form.value.id)
}

const emit = defineEmits(['onResourceSelect'])

onMounted(()=>{
    onGetData()
})

</script>

<style scoped>

</style>